package com.atguigu.gmall.product.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.BaseCategoryViewService;
import com.atguigu.gmall.product.service.ProductApiService;
import com.atguigu.gmall.product.service.SkuManageService;
import com.atguigu.gmall.product.service.SpuManageService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/product")
public class ProductApiController {
    @Autowired
    private ProductApiService productApiService;
    @Autowired
    private BaseCategoryViewService baseCategoryViewService;
    @Autowired
    private SkuManageService skuManageService;
    @Autowired
    private SpuManageService spuManageService;

    /**
     * 根据skuId获取SkuInfo  包含Image
     *
     * @param skuId
     * @return
     */

    @GetMapping("/inner/getSkuInfo/{skuId}")
    public SkuInfo getSkuInfoAndImage(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = productApiService.getSkuInfoAndImage(skuId);
        return skuInfo;
    }


    /**
     * 根据三级分类id获取分类信息
     *
     * @param category3Id
     * @return
     */
    @GetMapping("/inner/getCategoryView/{category3Id}")
    @GmallCache(prefix = "categoryView:")
    public BaseCategoryView getCategoryView(@PathVariable Long category3Id) {
        BaseCategoryView categoryView = baseCategoryViewService.getById(category3Id);
        return categoryView;
    }

    /**
     * 根据skuId 获取最新的商品价格
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getSkuPrice/{skuId}")

    public BigDecimal getSkuPrice(@PathVariable("skuId") Long skuId) {
        return skuManageService.getSkuPrice(skuId);
    }

    /**
     * 根据spuId 获取海报数据
     *
     * @param spuId
     * @return
     */
    @GetMapping("/inner/findSpuPosterBySpuId/{spuId}")
    @GmallCache(prefix = "SpuPosterBySpuId:")
    public List<SpuPoster> findSpuPosterBySpuId(@PathVariable("spuId") Long spuId) {
        List<SpuPoster> spuPosterList = spuManageService.findSpuPosterBySpuId(spuId);
        return spuPosterList;
    }

    /**
     * 根据skuId 获取平台属性数据
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getAttrList/{skuId}")
    @GmallCache(prefix = "spuSaleAttrListCheckBySku:")
    public List<BaseAttrInfo> getAttrList(@PathVariable Long skuId) {
        List<BaseAttrInfo> list = skuManageService.getAttrList(skuId);
        return list;
    }

    /**
     * 根据spuId,skuId 获取销售属性数据
     * @param skuId
     * @param spuId
     * @return
     */
      @GetMapping("/inner/getSpuSaleAttrListCheckBySku/{skuId}/{spuId}")
      @GmallCache(prefix = "attrList:")
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(@PathVariable Long skuId,@PathVariable Long spuId){
          List<SpuSaleAttr> list = spuManageService.getSpuSaleAttrListCheckBySku(skuId, spuId);
          return list;
      }

    /**
     * 根据spuId 获取到销售属性值Id 与skuId 组成的数据集
     * @param spuId
     * @return
     */
      @GetMapping("/inner/getSkuValueIdsMap/{spuId}")
      @GmallCache(prefix = "skuValueIdsMap:")
    public Map<String,String> getSkuValueIdsMap(@PathVariable Long spuId){
          Map<String, String> valueJsonMap = skuManageService.getSkuValueIdsMap(spuId);
          return valueJsonMap;
      }

    /**
     * 查询首页的页面的信息
     * @return
     */

    @GetMapping("/inner/getBaseCategoryList")
    @GmallCache(prefix = "baseCategoryList:")
    public List<JSONObject> getBaseCategoryList(){
          return baseCategoryViewService.getBaseCategoryList();
}

    /**
     * 根据品牌Id 获取品牌数据
     * @param tmId
     * @return
     */
    @GetMapping("/inner/getTrademark/{tmId}")
    @GmallCache(prefix = "trademark:")
    public BaseTrademark getTrademark(@PathVariable("tmId") Long tmId){
        return skuManageService.getTrademark(tmId);
    }


}
